// import com.jcraft.jsch.*;
// import java.sql.*;

// public class DBUpdater {
	// public static void main(String[] args) {
		// // JSch SSH code
		// /*try
		// {
			// JSch.setConfig("StrictHostKeyChecking", "no");	// Don't worry about the key being ok.
			// JSch jsch = new JSch();
			// Session session = jsch.getSession("marshc4", "128.113.91.227", 22);
			// session.setPassword("zq3g7u.9");
			// session.connect(30000);
			
			// Channel channel = session.openChannel("shell");
			// channel.setInputStream(System.in);
			// channel.setOutputStream(System.out);
			// channel.connect(30000);
		// }
		// catch (Exception e)
		// {
			// System.out.println(e);
		// }*/
		
		// // JDBC SQL connection code
		// /*final String DB_URL = "jdbc:mysql://128.113.91.227/";
		// final String USER = "marshc4_admin";
		// final String PASS = "zq3g7u.9";
		
		// Connection conn = null;
		// Statement stmt = null;
		// try
		// {
			// Class.forName("com.mysql.jdbc.Driver");
			// conn = DriverManager.getConnection(DB_URL, USER, PASS);
		// }
		// catch (SQLException e)
		// {
			// e.printStackTrace();
		// }
		// catch (Exception e)
		// {
			// e.printStackTrace();
		// }*/
		
		// // Connect via SSH.
		// int lport = 3307;//4321;
		// String rhost = "74.220.207.182";//"128.113.91.227";
		// int rport = 3306;
		
		// String ssh_user = "marshpro";//"marshc4";
		// String ssh_password = "@Ignistica15";//"zq3g7u.9";
		// String ssh_host = "74.220.207.182";//"128.113.91.227";
		// int port = 22;
		// /*try
		// {
			// JSch jsch = new JSch();
			// Session session = jsch.getSession(ssh_user, ssh_host, port);
			// session.setPassword(ssh_password);
			// session.setConfig("StrictHostKeyChecking", "no");
			// System.out.println("Establishing SSH connection...");
			// session.connect();
			// int assignedPort = session.setPortForwardingL(lport, rhost, rport);
			// System.out.println("localhost:" + assignedPort + " -> " + rhost + ":" + rport);
			// System.out.println("Got here.");
		// }
		// catch (Exception e)
		// {
			// e.printStackTrace();
		// }*/
		
		// System.out.println("Then here.");
		// // Connect to SQL DB.
		// Connection con = null;
		// String driver = "com.mysql.jdbc.Driver";
		// String url = "jdbc:mysql://" + "74.220.207.182:3306/marshpro_math";//+ "localhost" + ":" + lport;// + "/marshc4_test";// + "/";
		// String dbUser = "marshpro_marsh";//"marshc4_admin";
		// String dbPassword = "marshc3793";//"zq3g7u.9";
		// try
		// {
			// Class.forName(driver);
			// System.out.println("About to connect.");
			// con = DriverManager.getConnection(url, dbUser, dbPassword);
			
			// System.out.println("Connected.");
			// /*Statement stmt = con.createStatement();
			// String sql = "CREATE DATABASE test_db";
			// System.out.println("About to run statement.");
			// int update = stmt.executeUpdate(sql);
			// System.out.println("Statement run.");*/
		// }
		// catch (SQLException e)
		// {
			// e.printStackTrace();
		// }
		// catch (Exception e)
		// {
			// e.printStackTrace();
		// }
	// }
// }

import com.jcraft.jsch.*;
import java.io.*;

public class DBUpdater {
	public static void main(String[] args) {
		String ssh_user = "marshpro";//"marshc4";
		String ssh_password = "@Ignistica15";//"zq3g7u.9";
		String ssh_host = "74.220.207.182";//"128.113.91.227";
		int ssh_port = 22;
		
		String ssh_command = "mysql -h localhost -u marshpro_marsh -pmarshc3793 marshpro_math < sql_file.sql";
		
		try {
			JSch.setConfig("StrictHostKeyChecking", "no");
			JSch jsch = new JSch();
			
			//Connect with ssh session.
			System.out.println("Establishing SSH session...");
			Session session = jsch.getSession(ssh_user, ssh_host, ssh_port);
			session.setPassword(ssh_password);
			session.connect();
			System.out.println("SSH session has been established.");
			
			System.out.println("Executing command over SSH...");
			ExecResult result = execCommand(session, ssh_command);
			System.out.println("Command execution complete.");
			System.out.println("Return Value: " + result.returnValue);
			System.out.println("Output: " + result.output);
			
			session.disconnect();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static ExecResult execCommand(Session session, String commandStr) {
		DBUpdater dbu = new DBUpdater();
		ExecResult result = dbu.new ExecResult();
		result.output = "";
		
		try {
			//Open an 'exec' channel to execute commands over ssh.
			Channel channel = session.openChannel("exec");
			
			// Set the command to execute and the io streams.
			((ChannelExec)channel).setCommand(commandStr);
			channel.setInputStream(null);
			((ChannelExec)channel).setErrStream(System.err);
			InputStream in = channel.getInputStream();
			
			// Connect to the channel
			channel.connect();
			
			final int arr_Size = 1024;
			byte[] tmp = new byte[arr_Size];
			while(true) {
				while(in.available() > 0) {
					int i = in.read(tmp, 0, arr_Size);
					if (i < 0) break;
					result.output += new String(tmp, 0, i);
				}
				if (channel.isClosed()) {
					result.returnValue = channel.getExitStatus();
					break;
				}
				try {
					Thread.sleep(1000);
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			}
			
			channel.disconnect();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
			
		return result;
	}
	
	private class ExecResult {
		public int returnValue;
		public String output;
	}
}